﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MotionCS.Helper
{
    public static class FloatComparison
    {
        // 常用的容差值
        public const float DefaultTolerance = 1e-6f;    // 0.000001
        public const float LooseTolerance = 1e-3f;      // 0.001
        public const float StrictTolerance = 1e-9f;   // 0.000000001

        /// <summary>
        /// 判断float是否等于零（使用默认容差）
        /// </summary>
        public static bool IsZero(this float value)
        {
            return Math.Abs(value) < DefaultTolerance;
        }

        /// <summary>
        /// 判断float是否等于零（使用指定容差）
        /// </summary>
        public static bool IsZero(this float value, float tolerance)
        {
            return Math.Abs(value) < tolerance;
        }

        /// <summary>
        /// 判断float是否近似等于另一个值
        /// </summary>
        public static bool Approximately(this float a, float b, float tolerance = DefaultTolerance)
        {
            return Math.Abs(a - b) < tolerance;
        }
    }
}
